//
//  HMTabBar.swift
//  微博
//
//  Created by 程双岐 on 16/9/19.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

protocol HomeDelegate : NSObjectProtocol {
    func tabBarButtonAction()
    
}

class HMTabBar: UITabBar {


    
    
    var block : (()->())?
    
    weak var hmDelegate :HomeDelegate?
    
    
    private lazy var button: UIButton = {
    let btn = UIButton()
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .selected)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .selected)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        return btn
        
    }()
    
    
    
    override init(frame:CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func buttonAction() {
        print("haha")
        block?()
        hmDelegate?.tabBarButtonAction()
    }
    
    private func setupUI() {
        addSubview(button)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        button.centerY = height * 0.5
        button.centerX = width * 0.5
        let itemWidth = width / 5
        var idx = 0
        
        
        for value in subviews
        {
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                value.width = itemWidth
                value.x = itemWidth * CGFloat(idx)
                idx += 1
                if idx == 2 {
                    idx += 1
                }
            }
        }
        
        
    }
    

}
